/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: ui.hpp
 */

#ifndef _UI_HPP_
#define _UI_HPP_

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>

#include "runner.hpp"
#include "workflow.hpp"
#include "manager.hpp"
#include "generic_exception.hpp"
#include "xml_syn_logging.hpp"

//-----------------------------------------------------------------------------
// Enum, struct, aliases
namespace middleware {
/*! \struct UIConfig
 * \brief Configuration for UI.
 *
 * Configuration dtructure fo UI.
 */
typedef struct {
	/*!
	 * Pointer to Manager in order to call methods for enqueueing a Workflow.
	 */
	Manager* ptrto_manager; /* To enqueue a workflow */
	/*!
	 * Logger
	 */
	logging::XmlSynLogger* _xml_logger;
} UIConfig;
}
//-----------------------------------------------------------------------------
// Constants
namespace middleware {
/*!
 * Number of maximum tasks in a Workflow to be randomly generated.
 */
const unsigned int kUIMaxTasksInWorkflow = 5;
/*!
 * Highest number of seconds to wait for random generation of a Workflow:
 * Workflow production max period in seconds.
 */
const unsigned int kUIMaxWorkflowGeneretionPeriod = 30;
/*!
 * Etiquette for output.
 */
const std::string kUIEtq = "UI:        ";
/*!
 * Etiquette for output.
 */
const std::string kUI1Etq = "DataRequestManager:        ";
}
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*! \class UI
 * \brief Class for UI component.
 *
 * Implements elements necessary for UI component to run.
 */
class UI : public Runner {
public:
	/*!
	 * Constructs a new, non-configured UI. Calling this constructor causes
	 * pointer to Manager to be null and log postfix to be statically generated
	 */
	UI();
	/*!
	 * Constructs a new, well-configured UI through passed configuration dtructure.
	 */
	UI(UIConfig conf);
	/*!
	 * Destructor.
	 */
	~UI();
	/*!
	 * Runs UI. If another thread was running, it will be destoyed.
	 */
	void run() {
		this->_ui_thread.reset();
		this->_ui_thread = boost::shared_ptr<boost::thread>(
				new boost::thread(
						boost::bind(&UI::exec, this)));
	}
	/*!
	 * Joins to this thread and wait for it to finish. When calling run(), user
	 * should immediately call this function in order to let the current thread
	 * not to die and terminate child.
	 */
	void join() {
		(this->_ui_thread)->join();
	}
private:
	/*!
	 * Creates a random variable number of tasks, put the in a workflow and
	 * returns it. This function contains a finite loop and must be called an
	 * infinite number of times. Attention, workflow is created dynamically,
	 * is up to uset to clear it.
	 */
	Workflow* CreateWorkflow();
	/*!
	 * Execution function. Sends workflows.
	 */
	void exec();
	/*!
	 * Initialization routine with user.
	 */
	void bonjour() throw(GenericException);
	/*!
	 * Configuration.
	 */
	UIConfig _config;
	/*!
	 * Undrlaying thread. This sends workflows.
	 */
	boost::shared_ptr<boost::thread> _ui_thread;
}; /* UI */
} /* middleware */

#endif
